# -*- coding: utf-8 -*-
# @Time : 2025/6/24 下午3:18
# @Author : cc 
# @Email : liuchenchen@starmerx.com
# @Project : my-tools
# @Desc : 功能描述
from PyQt5.QtWidgets import QApplication, QFrame, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QTimer, QTime, QRect
from PyQt5.QtGui import QPainter, QColor, QLinearGradient, QFont


class DocumentScanner(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFrameStyle(QFrame.Box | QFrame.Plain)
        self.setLineWidth(2)
        self.setStyleSheet("border-color: #555;")
        self.setMinimumSize(400, 500)

        self.scan_pos = 0
        self.scan_width = 20
        self.scan_direction = 1
        self.scan_speed = 2

        # 模拟文档内容
        self.document_lines = [
            "文档标题",
            "",
            "这是一份模拟扫描的文档",
            "包含多行文字内容",
            "",
            "第1段：演示扫描效果",
            "第2段：动态扫描动画",
            "",
            "扫描进度显示在右侧",
            "",
            "最后一行内容"
        ]

        self.font = QFont("Arial", 12)
        self.scan_timer = QTimer(self)
        self.scan_timer.timeout.connect(self.update_scan)
        self.scan_timer.start(30)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setFont(self.font)

        # 绘制文档背景
        painter.fillRect(self.rect(), QColor(240, 240, 240))

        # 绘制文档文字
        painter.setPen(Qt.black)
        for i, line in enumerate(self.document_lines):
            painter.drawText(20, 40 + i * 30, line)

        # 绘制扫描区域
        scan_rect = QRect(0, self.scan_pos - self.scan_width // 2,
                          self.width(), self.scan_width)

        gradient = QLinearGradient(scan_rect.topLeft(), scan_rect.bottomLeft())
        gradient.setColorAt(0, QColor(100, 255, 100, 30))
        gradient.setColorAt(0.5, QColor(50, 255, 50, 150))
        gradient.setColorAt(1, QColor(100, 255, 100, 30))

        painter.fillRect(scan_rect, gradient)

        # 绘制扫描进度条
        painter.setPen(QColor(0, 180, 0))
        progress_width = int((self.scan_pos / self.height()) * 30)
        painter.drawRect(self.width() - 40, 20, 30, self.height() - 40)
        painter.fillRect(self.width() - 40, 20 + self.height() - 40 - self.scan_pos,
                         30, progress_width, QColor(0, 200, 0, 150))

        # 绘制当前扫描时间
        current_time = QTime.currentTime().toString("hh:mm:ss")
        painter.drawText(self.width() - 120, self.height() - 20, f"扫描中: {current_time}")

    def update_scan(self):
        self.scan_pos += self.scan_speed * self.scan_direction

        if self.scan_pos >= self.height() + self.scan_width:
            self.scan_direction = -1
        elif self.scan_pos <= -self.scan_width:
            self.scan_direction = 1

        self.update()


class ScannerApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("文档扫描模拟器")

        self.scanner = DocumentScanner()

        layout = QVBoxLayout()
        layout.addWidget(self.scanner)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.resize(600, 600)


if __name__ == "__main__":
    app = QApplication([])
    window = ScannerApp()
    window.show()
    app.exec_()